package com.protocols.sctp.m3ua.sccp;

import java.io.Serializable;

import com.protocols.BinaryInteger;
import com.protocols.DummyPayload;
import com.protocols.GeneralProtocolData;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;
import com.protocols.sctp.m3ua.M3uaPayLoadData;
import com.protocols.sctp.m3ua.ProtocolData;
import com.protocols.sctp.m3ua.sccp.tcap.TcapBegin;
import com.protocols.sctp.m3ua.sccp.tcap.TcapContinue;

public class SccpUnitDataMsg implements GeneralProtocolData, Serializable{

	private OctaveInteger MSgType = new OctaveInteger(9,1);	// Unit Data
	private BinaryInteger SccpClass = new BinaryInteger(4,0); //Connectionless Services
	private BinaryInteger MsgHandling = new BinaryInteger(4,4);
	private OctaveInteger CalledPartyLocation = new OctaveInteger(1);
	private OctaveInteger CallingPartyLocation = new OctaveInteger(1);
	private OctaveInteger PayLoadLocation = new OctaveInteger(1);
	private OctaveInteger CalledPartyLength = new OctaveInteger(1);
	public CallPartyAddress CalledParty = new CallPartyAddress();
	private OctaveInteger CallingPartyLength = new OctaveInteger(1);
	public CallPartyAddress CallingParty = new CallPartyAddress();		
	private OctaveInteger PayLoadLength = new OctaveInteger(1);
	public GeneralProtocolData Payload = new DummyPayload();
	
	public SccpUnitDataMsg() {
				
	}

	public void toggleDestination() throws ProtocolStackException{
		
		byte [] calledbyte=CalledParty.getByteArray();
		byte [] callingbyte=CallingParty.getByteArray();
		CalledParty.decode(ProtoOperations.concatArrays(new byte[]{(byte)callingbyte.length}, callingbyte));
		CallingParty.decode(ProtoOperations.concatArrays(new byte[]{(byte)calledbyte.length}, calledbyte));
	}
	public void setSccpClassValue(int classVal){
		SccpClass.setValue(classVal);		
	}

	
	public int getSccpClassValue(){
		return SccpClass.getValue();
	}
	
	public void setMsgHandling(int msgHandling){
		MsgHandling.setValue(msgHandling);
	}
	
	public int getMsgHandling(){
		return MsgHandling.getValue();
	}
	

	public byte [] getByteArray(){		
		byte [] classMsgHAndling = {(byte)(MsgHandling.getOffsetValue()|SccpClass.getOffsetValue())};		
		byte [] calledparty = CalledParty.getByteArray();
		byte [] callingparty = CallingParty.getByteArray();
		byte [] payload = this.Payload.getByteArray();
		PayLoadLength.setValue(payload.length);
		CalledPartyLocation.setValue(3);
		CalledPartyLength.setValue(calledparty.length);
		CallingPartyLocation.setValue(3+calledparty.length);
		CallingPartyLength.setValue(callingparty.length);
		PayLoadLocation.setValue(3+calledparty.length+callingparty.length);
		return  ProtoOperations.concatArrays( MSgType.getByteArray(), classMsgHAndling, CalledPartyLocation.getByteArray(), CallingPartyLocation.getByteArray(), PayLoadLocation.getByteArray(), CalledPartyLength.getByteArray(), calledparty, CallingPartyLength.getByteArray(),callingparty,PayLoadLength.getByteArray(),payload );

	}

	public int getLength() {
		
		return getByteArray().length;
	}

	public byte[] decode(byte [] input) throws ProtocolStackException{
		
		if (input[0]==9 && (input[1]&0b00001111)==1 ){			
			
			setMsgHandling((input[1]&0b11110000)>>4);
			CalledPartyLocation.setValue(input[2]);
			CallingPartyLocation.setValue(input[3]);
			PayLoadLocation.setValue(input[4]);
			
			byte []remainder = ProtoOperations.extractByteArray(input, 5, input.length-5);
			remainder= CalledParty.decode(remainder);
			remainder= CallingParty.decode(remainder);
			
			return remainder;
	
		}
		else
			throw new ProtocolStackException("Decode Failure:SCCPUnitData"+ProtoOperations.getHexString(input));
		
		
		
	}	
	
	

}
